package com.balance.controller.sys;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.balance.pojo.common.CommonResult;
import com.balance.pojo.sys.SysUser;
import com.balance.service.sys.ISysUserService;

import com.balance.utils.UserIPAddrUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

/**
 * @Title: SysUserController
 * @Author 小东
 * @Package com.balance.controller
 * @Date 2025/3/27 18:14
 * @description: 系统用户控制层
 */
@RestController
@RequestMapping("sys/user")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 获取全部数据 or 检索条件查询
     * @param sysUser
     * @return
     */
    @GetMapping("/list")
    public CommonResult<List<SysUser>> getAll(SysUser sysUser, HttpServletResponse response) throws UnknownHostException {
        if(sysUser == null){
            throw new IllegalArgumentException("参数有误,请输入正确参数");
        }else{
            System.out.println(UserIPAddrUtil.getIp());
            return CommonResult.success(iSysUserService.getAll(sysUser));
        }
    }

    /**
     * 根据ID查询对应信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult<SysUser> getUserById(@PathVariable Integer id){
        if( id == null){
            throw new IllegalArgumentException("参数有误,请输入正确参数");
        }else{
            return CommonResult.success(iSysUserService.getUserById(id));
        }
    }

    /**
     * 动态查询用户信息和权限信息列表
     * @param params
     * @return
     */
    @GetMapping("/infoRole")
//    @SaIgnore
    public SysUser selectUserAndRoleList(@RequestParam Map<String, Object> params) {
        return iSysUserService.selectUserAndRoleList(params);
    }


    // 新增用户
    @PostMapping
    int insertUser(@RequestBody SysUser user){
        return iSysUserService.insertUser(user);
    }


    // 更新用户
    @PutMapping
    int updateUser(@RequestBody SysUser user){
        return iSysUserService.updateUser(user);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    int deleteUserById(@PathVariable Long id){
        return iSysUserService.deleteUserById(id);
    }

    @PostMapping("/check")
    public SysUser getSysUserByUsername(@RequestBody SysUser sysUser){
        return iSysUserService.getSysUserByUsername(sysUser);
    }
}
